package com.css.apps.base.sysjob.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.sysjob.model.SysJob;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

public class DelOrgJobSysRoles extends AdminAction{
	private static final long serialVersionUID = 3119149111465042268L;
	private static Log log = LogFactory.getLog(DelOrgJobSysRoles.class);
	private String orgId;
	private String jobId;
	private String ids;
	
	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			List<String> sysIds = StringHelper.strToList(ids);
			if(ListUtil.isLstEmpty(sysIds) || StringHelper.isEmpty(orgId) || StringHelper.isEmpty(jobId)){
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));	
				return Action.ERROR;
			}
			List<Object> delLst = new ArrayList<>();
			List<String> delIds = new QueryCache("select a.uuid from SysJob a where a.orgId =:orgId and a.jobId =:jobId and a.sysId in (:sysIds) ")
					.setParameter("orgId", orgId).setParameter("jobId", jobId).setParameter("sysIds", sysIds).list();
			if (!ListUtil.isLstEmpty(delIds)) {
				delLst.addAll(QueryCache.idToObj(SysJob.class, delIds));
			}
			tx = new TransactionCache();
			tx.delete(delLst);
			tx.commit();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			log.error(ex.getMessage(), ex);
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}